package br.com.news.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import br.com.news.utils.database.beans.CategoriaBean;
import br.com.news.utils.service.exception.ServiceException;

@Path("/Categoria")
public class CategoriaWs extends GenericWs<CategoriaBean>{
	
	private static final Logger logger = Logger.getLogger(CategoriaWs.class);
	
	@GET
	@Path("/categoriaAtivas")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String categoriaAtivas(){
		try {
			return listToJson(RmiFactory.getCategoriaService().findCategoriaAtiva());
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
	
	@GET
	@Path("/findAll")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String findAll() {
		try {
			return listToJson(RmiFactory.getCategoriaService().findAll());
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
	
	@GET
	@Path("/findAllXml")
	@Produces("application/xml")
	public List<CategoriaBean> findAllXml() {
		try {
			return RmiFactory.getCategoriaService().findAll();
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	
	@GET
	@Path("/categoriasMaisAcessadas")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String categoriasMaisAcessadas() {
		try {
			return listToJson(RmiFactory.getCategoriaService().categoriasMaisAcessadas());
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
	
	@POST
	@Path("/findByExample")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Consumes(MediaType.APPLICATION_JSON)
	public String findByExample(String categoria) {
		CategoriaBean categoriaBean = (CategoriaBean) convertFromJson(categoria, new CategoriaBean());
		return  listToJson(RmiFactory.getCategoriaService().findByExample(categoriaBean));
	}
	
	@GET
	@Path("/findCategoriaByStatus/{status}")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String findCategoriaByStatus(@PathParam(value="status") boolean status) {
		try {
			return listToJson(RmiFactory.getCategoriaService().findCategoriaByStatus(status));
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			return "NOK";
		}
	}
	
	@PUT
	@Path("/update")
	@Consumes("application/json")
	public void update(String categoria) {
		CategoriaBean categoriaBean = (CategoriaBean) convertFromJson(categoria, new CategoriaBean());
		try {
			RmiFactory.getCategoriaService().update(categoriaBean);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}
	}
	
	@PUT
	@Path("/insert")
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public void save(String json) {
		try {
			CategoriaBean categoriaBean = (CategoriaBean) convertFromJson(json, new CategoriaBean());
			RmiFactory.getCategoriaService().save(categoriaBean);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}
	}
	
	
}
